﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Catering_Rules;

namespace Catering.UCPanle
{
    public partial class DDLTool : System.Web.UI.UserControl
    {
        private string AREAID = "";
        private string CLASSID = "";
        private string Year = "";
        private bool m_ISAdminAll = true;
        FromRecords frRules = null;
        private string m_SecondCheck = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy'})");
                loadPage();
            }
        }
        private void loadPage()
        {
            frRules = new FromRecords();
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                if (!isAdmin && !string.IsNullOrEmpty(m_SecondCheck))
                {
                    string[] split = m_SecondCheck.Split('|');
                    if (split.Length > 1)
                    {
                        if (Catering.Toolss.IsVerification(Session["RoleID"].ToString(), split[0], split[1])) isAdmin = true;
                    }
                }
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_Area, isAdmin, groupid, companyid, m_ISAdminAll);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class, isAdmin, classid, m_ISAdminAll);
            }

            //List<string> listyears = frRules.GetYears();
            //Bind_Data_to_Controls.BindListBox(listyears, ddl_Year);
        }

        /// <summary>
        /// 进行第二次检测
        /// </summary>
        public string SecondCheck
        {
            get { return (String)ViewState["allowDuplicates"]; }
            set { this.m_SecondCheck = value; }
        }

        /// <summary>
        /// 获得年
        /// </summary>
        /// <returns></returns>
        public string GetYear()
        {
            if (!string.IsNullOrEmpty(TextBox1.Text))
            {
                Year = TextBox1.Text;
            }
            return Year;
        }

        /// <summary>
        /// 获取作业区ID
        /// </summary>
        /// <returns></returns>
        public string GetDDL_Area()
        {
            if (ddl_Area.SelectedItem != null)
            {
                AREAID = ddl_Area.SelectedItem.Value;
            }
            return AREAID;
        }

        #region 初始化
        
        public void SetAreaID(string AreaID)
        {
            this.ddl_Area.SelectedValue = AreaID;
        }

        public void SetClassID(string ClassID)
        {
            this.ddl_class.SelectedValue = ClassID;
        }

        public void SetYear(string year)
        {
            this.TextBox1.Text = year;
        }

        #endregion

        /// <summary>
        /// 是否显示年
        /// </summary>
        public Boolean DisplayYear
        {
            get
            {
                return (Boolean)ViewState["allowDuplicates"];
            }
            set
            {
                Label1.Visible = value;
                TextBox1.Visible = value;
            }
        }

        /// <summary>
        /// 是否显示年
        /// </summary>
        public Boolean ISAdminAll
        {
            get { return (Boolean)ViewState["allowDuplicates"]; }
            set { this.m_ISAdminAll = value; }
        }

        /// <summary>
        /// 获取班组ID
        /// </summary>
        /// <returns></returns>
        public string GetDDL_Class()
        {
            if (ddl_class.SelectedItem != null)
            {
                CLASSID = ddl_class.SelectedItem.Value;
            }
            return CLASSID;
        }
        protected void ddl_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Area.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class);
            }
        }

        protected void ddl_class_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (ddl_Year.SelectedItem != null)
        //    {
        //        Year = ddl_Year.SelectedItem.Value;
        //    }
        //}

        public Dictionary<int, string> GetAllDDL_Area()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            foreach (ListItem item in ddl_Area.Items)
            {
                if (!string.IsNullOrEmpty(item.Value) && !item.Value.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(item.Value), item.Text);
                }
            }
            return result;
        }

        public Dictionary<int, string> GetAllDDL_Class()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            foreach (ListItem item in ddl_class.Items)
            {
                if (!string.IsNullOrEmpty(item.Value) && !item.Value.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(item.Value), item.Text);
                }
            }
            return result;
        }
    }
}